<template>
  <view>
    <navigator open-type="navigate" url="/pages/search/index">
      <SearchBtn></SearchBtn>
    </navigator>
    <view>
      <view class="goods-list">
        <view
          v-for="(goods, i) in goodsList"
          :key="i"
          @click="goGoodsDetail(goods.goods_id)"
        >
          <view class="goods-item">
            <!-- 商品左侧图片区域 -->
            <view class="goods-item-left">
              <image
                :src="goods.goods_small_logo || defaultPic"
                class="goods-pic"
                mode="widthFill"
              ></image>
            </view>
            <!-- 商品右侧信息区域 -->
            <view class="goods-item-right">
              <!-- 商品标题 -->
              <view class="goods-name">{{ goods.goods_name }}</view>
              <view class="goods-info-box">
                <!-- 商品价格 -->
                <view class="goods-price"
                  >￥{{ goods.goods_price || tofixed }}</view
                >
              </view>
            </view>
          </view>
        </view>
      </view>
    </view>
  </view>
</template>

<script>
import { getGoodsListAPI } from "@/api/goods_list";

export default {
  data() {
    return {
      queryObj: {
        // 查询关键字
        query: "",
        cid: "",
        page: 1,
        pagesize: 10,
      },
      goodsList: [],
      // 加载失败默认 图片
      defaultPic:
        "https://hbimg.huabanimg.com/8925d22f35f231cca7c3be55d84886e370adb8245220-F5hlPa_fw658",
      // 节流
      isLoading: false,
    };
  },

  filters: {
    tofixed(num) {
      return Number(num).toFixed(2);
    },
  },
  onLoad(options) {
    this.queryObj.query = options.query;
    this.queryObj.cid = options.cid;
    this.getGoodsList();
  },
  methods: {
    async getGoodsList(cd) {
      this.isloading = true;
      const res = await getGoodsListAPI(this.queryObj);
      this.isloading = false;

      cd && cd();

      this.goodsList = [...this.goodsList, ...res.message.goods];
      this.total = res.message.total;
    },
    // 跳转商品详情页
    goGoodsDetail(id) {
      wx.navigateTo({
        url: "/pages/goods_detail/index?goods_id=" + id,
      });
    },
  },
  //下拉触底事件
  onReachBottom() {
    if (this.isLoading) return;
    if (this.queryObj.page * this.queryObj.pagesize >= this.total) return;
    this.queryObj.page += 1;
    this.getGoodsList();
  },
  // 下拉刷新的事件
  onPullDownRefresh() {
    // 1.重置请求数据
    this.queryObj.page = 1;
    this.total = 0;
    this.isLoading = false;
    this.goodsList = [];

    //重新发送请求
    this.getGoodsList(() => {
      uni.stopPullDownRefresh();
    });
  },
};
</script>

<style lang="scss" scoped>
.goods-item {
  display: flex;
  border-bottom: 1px solid #f0f0f0;
  padding: 20rpx 10rpx;

  .goods-item-left {
    margin-left: 10rpx;
    margin-right: 10rpx;
    image {
      width: 200rpx;
      height: 200rpx;
      display: block;
    }
  }
  .goods-item-right {
    display: flex;
    flex-direction: column;
    justify-content: space-between;

    .goods-name {
      font-size: 28rpx;
    }

    .goods-price {
      font-size: 34rpx;
      color: #c00000;
    }
  }
}
</style>